import java.io.*;
import java.lang.management.ManagementFactory;
import java.net.*;
import Messages.OSTypeEnum;
import com.sun.management.OperatingSystemMXBean;

public class DataType {
	public static String ping(String Addr) {
		try {
			InetAddress address = InetAddress. getByName(Addr);
			// debug info
			System.out.println("Name: " + address.getHostName());
			System.out.println("Addr: " + address.getHostAddress());

			if(address.isReachable(3000)) {
				System.out.println("Reached!");
				return "100";
			}
		}
		catch (UnknownHostException e) {
			System.err.println("Unable to lookup web.mit.edu");
			return "0";
		}
		catch (IOException e) {
			System.err.println("Unable to reach web.mit.edu");
			return "0";
		}
		return "0";
	}
	
	public static String cpu(OSTypeEnum Os) {
		OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(
				OperatingSystemMXBean.class);

		/* What % load the overall system is at, from 0.0-1.0 */
    	double cpuUsage = osBean.getSystemCpuLoad();
    	 
    	/* 0-100% */
    	int res = (int)((cpuUsage) * 100);

		System.out.println("CPU Usage %:: " + Integer.toString(res));
		return Integer.toString(res);
	}

	public static String memory(OSTypeEnum Os) {
		OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(
				OperatingSystemMXBean.class);
	
		/* get total and free memory */
		double totalSpace = osBean.getTotalPhysicalMemorySize(); //total memory in bytes.
    	double freeSpace = osBean.getFreePhysicalMemorySize(); //unallocated / free memory in bytes.
    	double usedSpace = totalSpace - freeSpace;
 
    	/* allocated / free memory */
    	int res = (int)((usedSpace / totalSpace) * 100);

    	System.out.println("Memory Usage  %:: " + Integer.toString(res));
		return Integer.toString(res);
	}

	public static String disk(OSTypeEnum Os) {
		File file;
		
		switch(Os) {
		case Windows:
			file = new File("c:");
			break;
		case Mac:
		case Unix:
		case Solaris:
		default:
			file = new File("/");
			break;
		}
		
		/* Get total and free space */
    	double totalSpace = file.getTotalSpace();
    	double freeSpace = file.getFreeSpace(); 
    	double usedSpace = totalSpace - freeSpace;

    	/* Allocated / free disk space */
    	int res = (int)((usedSpace / totalSpace) * 100);
    	
		System.out.println("Disk Free %:: " + Integer.toString(res));
		return Integer.toString(res);
	}
}
